<?php
function litimgurls($imgid=0)
{
    global $lit_imglist,$dsql;
    //获取附加表
    $row = $dsql->GetOne("SELECT c.addtable FROM #@__archives AS a LEFT JOIN #@__channeltype AS c 
                                                            ON a.channel=c.id where a.id='$imgid'");
    $addtable = trim($row['addtable']);
    
    //获取图片附加表imgurls字段内容进行处理
    $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'");
    
    //调用inc_channel_unit.php中ChannelUnit类
    $ChannelUnit = new ChannelUnit(2,$imgid);
    
    //调用ChannelUnit类中GetlitImgLinks方法处理缩略图
    $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']);
    
    //返回结果
    return $lit_imglist;
}

//一般文章列表
function title_list($id, $limit = 10, $start = 0, $title_width = 270)
{
	global $dsql;
	
	$dsql->SetQuery("select * From cms_archives where typeid = '$id' order by id desc limit $start, $limit");
	$dsql->Execute();
	
	$title = '';
	while($row = $dsql->GetArray())
	{
		$title .= '<li class="clearfix" style="width:'.$width.'px; line-height:20px; text-overflow:ellipsis; white-space:nowrap; overflow:hidden; clear:both;"><a href="./info.php?id='.$row['id'].'" target="_blank">'.$row['title'].'</a></li>';
	}
	
	return $title;
}

//取得第一篇文章以及图片列表
function title_list_pic($id, $len=50)
{
	global $dsql;
	
	$dsql->SetQuery("select * From cms_archives where typeid = '$id' order by id desc limit 1");
	$dsql->Execute();
	
	$title = '';
	while($row = $dsql->GetArray())
	{
		$title .= '<dt><img src="./'.$row['litpic'].'"/></dt>';
		$title .= '<dd>'.SubTitle($row['description'], $len).'[详细]</dd>';
	}
	
	return $title;
}

//读取文章字数
function SubTitle($String,$Length,$showSym=1) {  
    if (mb_strwidth($String, 'UTF8') <= $Length ){  
        return $String;  
    }else{  
        $I = 0;  
        $len_word = 0;  
        while ($len_word < $Length){  
            $StringTMP = substr($String,$I,1);  
            if ( ord($StringTMP) >=224 ){  
                $StringTMP = substr($String,$I,3);  
                $I = $I + 3;  
                $len_word = $len_word + 2;  
            }elseif( ord($StringTMP) >=192 ){  
                $StringTMP = substr($String,$I,2);  
                $I = $I + 2;  
                $len_word = $len_word + 2;  
            }else{  
                $I = $I + 1;  
                $len_word = $len_word + 1;  
            }  
            $StringLast[] = $StringTMP;  
        }  
        /* raywang edit it for dirk for (es/index.php)*/  
        if (is_array($StringLast) && !empty($StringLast)){  
            $StringLast = implode("",$StringLast);

		if($showSym == 1)			
            $StringLast .= "..";  //
        }  
        return $StringLast;   
    }   
}

//一般文章列表
/*
{{id}} : id
{{title}} : 文章标题
{{date}} : 文章日期
{{len}} : 内容长度
{{shorttitle}} : 副标题
{{author}} : 作者
{{click}} : 点击数
{{litpic}} : 图片
{{description40}} : 图片
{{description30}} : 图片
{{description20}} : 图片
*/
function show_title_list($id, $tpl, $limit = 10, $start = 0, $title_len = 100, $date_format = "Y-m-d", $sql="", $content_len=80)
{
	global $dsql;
	
	//处理可以在多个栏目
	if(strpos($id, ',') !== false){
		$dsql->SetQuery("select a.* From cms_archives a, cms_arctiny b where a.typeid in(".$id.") and a.id=b.id and b.arcrank != '-2' $sql order by a.id desc limit $start, $limit");
	}else{
		$dsql->SetQuery("select a.* From cms_archives a, cms_arctiny b where a.typeid = '$id' and a.id=b.id and b.arcrank != '-2' $sql order by a.id desc limit $start, $limit");
	}

	$dsql->Execute();
	
	$str = '';

	while($row = $dsql->GetArray())
	{
		//print_r($row);
		if(strlen($row['title']) > $title_len){
			$row['title'] = SubTitle($row['title'], $title_len);
		}
		
		if(strlen($row['shorttitle']) > $shorttitle){
			$row['shorttitle'] = SubTitle($row['shorttitle'], $title_len);
		}
		
		$row['description'] = SubTitle($row['description'], $content_len);	

		$title = str_replace('{{title}}', $row['title'], $tpl);
		$title = str_replace('{{date}}', date($date_format, $row['senddate']), $title);
		$title = str_replace('{{len}}', strlen($row['description']), $title);
		$title = str_replace('{{shorttitle}}', $row['shorttitle'], $title);
		$title = str_replace('{{shorttitle50}}', SubTitle($row['shorttitle'], 50), $title);
		$title = str_replace('{{shorttitle80}}', SubTitle($row['shorttitle'], 80), $title);
		$title = str_replace('{{description}}', $row['description'], $title);
		$title = str_replace('{{author}}', $row['writer'], $title);
		$title = str_replace('{{click}}', $row['click'], $title);
		$title = str_replace('{{id}}', $row['id'], $title);
		$title = str_replace('{{litpic}}', $row['litpic'], $title);
		$title = str_replace('{{description100}}', SubTitle($row['description'], 100), $title);
		$title = str_replace('{{description180}}', SubTitle($row['description'], 180), $title);
		$title = str_replace('{{description80}}', SubTitle($row['description'], 80), $title);
		$title = str_replace('{{description40}}', SubTitle($row['description'], 40), $title);
		$title = str_replace('{{description30}}', SubTitle($row['description'], 30), $title);
		$title = str_replace('{{description20}}', SubTitle($row['description'], 20), $title);

		$str.= $title;
	}

	return $str;
}


/*
{{id}} : id
{{title}} : 文章标题
//从对应表中直接读取
*/
function show_title_list_form_mapping($id, $tpl, $limit = 10, $start = 0, $title_len = 100, $sql="")
{
	global $dsql;
	
	$dsql->SetQuery("select a.* From article_options_mapping a, cms_arctiny b where a.topicid = '$id' and a.article_id=b.id and b.arcrank != '-2' $sql order by a.id desc limit $start, $limit");
	$dsql->Execute();
	
	$str = '';
	while($row = $dsql->GetArray())
	{
		//print_r($row);
		if(strlen($row['article_title']) > $title_len){
			$row['article_title'] = SubTitle($row['article_title'], $title_len);
		}
		$title = str_replace('{{title}}', $row['article_title'], $tpl);
		$title = str_replace('{{id}}', $row['article_id'], $title);
		$str.= $title;
	}
	
	return $str;
}
